package com.tanhua.system.controller;

import com.tanhua.system.pojo.Freezing;
import com.tanhua.system.service.UserService;
import com.tanhua.system.vo.PageResult;
import com.tanhua.system.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("manage/users")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 模糊查询
     *
     * @param page
     * @param pagesize
     * @param id
     * @param nickname
     * @param city
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryUserData(
            @RequestParam("page") Integer page,
            @RequestParam("pagesize") Integer pagesize,
            @RequestParam("id") String id,
            @RequestParam("nickname") String nickname,
            @RequestParam("city") String city
    ) {
        try {
            PageResult pageResult = this.userService.queryUserData(page, pagesize, id, nickname, city);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询失败~", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 根据id查询用户基本资料
     *
     * @param userID
     * @return
     */
    @GetMapping("/{userID}")
    public ResponseEntity<UserInfoVo> queryUserInfoById(@PathVariable("userID") Long userID) {
        try {
            UserInfoVo userInfoVo = this.userService.queryUserInfoById(userID);
            if (null != userInfoVo) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("查询失败~", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户状态：冻结
     * @param params
     * @return
     */
    @PostMapping("freeze")
    public ResponseEntity freeze(@RequestBody Map<String, Object> params) {
        try {
            Freezing freezing = new Freezing();
            freezing.setUserId(Long.valueOf(params.get("userId").toString()));
            freezing.setFreezingTime(Integer.valueOf(params.get("freezingTime").toString()));
            freezing.setFreezingRange(Integer.valueOf(params.get("freezingRange").toString()));
            freezing.setReasonsForFreezing(params.get("reasonsForFreezing").toString());
            freezing.setFrozenRemarks(params.get("frozenRemarks").toString());
            Boolean freeze = this.userService.freeze(freezing);

            if (freeze){
                Map<String, Object> result = new HashMap<>();
                result.put("message", "冻结成功");
                return ResponseEntity.ok(result);
            }

        } catch (Exception e) {
            log.error("冻结失败~", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户状态：解冻
     * @param params
     * @return
     */
    @PostMapping("unfreeze")
    public ResponseEntity unFreeze(@RequestBody Map<String, Object> params){
        try {
            Freezing freezing = new Freezing();
            freezing.setUserId(Long.valueOf(params.get("userId").toString()));
            freezing.setReasonsForThawing(params.get("reasonsForThawing").toString());

            Boolean freeze = this.userService.unFreeze(freezing);

            if (freeze){
                Map<String, Object> result = new HashMap<>();
                result.put("message", "解冻成功");
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.error("解冻失败~", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
